﻿using System;
using wojilu.Apps.Qycms.Domain;
using wojilu.Members.Interface;
using wojilu.Members.Sites.Domain;
using wojilu.Members.Sites.Service;
using wojilu.Members.Users.Domain;
using wojilu.Web.Controller.Common;
using wojilu.Web.Mvc.Attr;

namespace wojilu.Web.Controller.Admin.Qycms
{
    public class RecomController : BaseController
    {
        public virtual IAdminLogService<SiteLog> logService { get; set; }

        public RecomController()
        {
            logService = new SiteLogService();
        }

        private void setpublic()
        {
            User user = ctx.viewer.obj as User;
            var modelPowerGroup = PowerGroup.findById(user.GroupId);
            bind("powergroup", modelPowerGroup);
        }

        [HttpGet, HttpPost]
        public virtual void RecomList()
        {
            SetPublic();
            setpublic();
            set("load", to(new AdminController().Load) + "?");
            var condition = " lng='" + DefaultLng + "'";
            DataPage<Document_Label> list = null;
            Int32 maxperpage = ctx.PostInt("MaxPerPage");
            Int32 page_id = ctx.PostInt("page_id") + 1;
            String limitkey = ctx.Get("limitkey");
            String limitclass = ctx.Get("limitclass");
            var mid = ctx.GetInt("mid");
            if (mid > 0)
            {
                condition += " and mid = " + mid;
            }
            if (strUtil.IsNullOrEmpty(limitkey)) limitkey = "id";
            if (strUtil.IsNullOrEmpty(limitclass)) limitclass = "desc";
            condition += " order by " + limitkey + " " + limitclass;
            CurrentRequest.setCurrentPage(page_id);
            list = Document_Label.findPage(condition, maxperpage);
            if (ctx.web.postValueAll().Count == 0)
            {
                ctx.web.ResponseWrite(list.RecordCount.ToString());
                ctx.web.ResponseEnd();
            }
            bind("list", list.Results);
        }

        [HttpGet]
        public virtual void RecomAdd()
        {
            SetPublic();
            setpublic();
            target(RecomAddDo);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            set("refalse", 1);
            set("tab", "true");
            var lng = DefaultLng;
            set("lng", lng);
            var list = Model.findAll();
            bind("list", list);
        }

        [HttpPost]
        public virtual void RecomAddDo()
        {
            Document_Label dl = new Document_Label();
            dl.lng = ctx.Post("lng");
            dl.mid = ctx.PostInt("mid");
            dl.labelname = ctx.Post("labelname");
            Result result = dl.insert();
            if (result.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("recommanage_add_log") + lang("log_extra_no"), "{Erros:'" + result.ErrorsText + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("recommanage_add_log") + lang("log_extra_ok"), "{Id:" + dl.Id + ", Name:'" + dl.labelname + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void RecomEdit(Int64 Id)
        {
            SetPublic();
            setpublic();
            target(RecomEditDo, Id);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            var recomread = Document_Label.findById(Id);
            bind("recomread", recomread);
        }

        [HttpPost]
        public virtual void RecomEditDo(Int64 Id)
        {
            Document_Label dl = Document_Label.findById(Id);
            dl.labelname = ctx.Post("labelname");
            Result result = dl.update();
            if (result.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("recommanage_edit_log") + lang("log_extra_no"), "{Erros:'" + result.ErrorsText + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("recommanage_edit_log") + lang("log_extra_ok"), "{Id:" + dl.Id + ", Name:'" + dl.labelname + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            ctx.web.ResponseEnd();
        }

        [HttpPost]
        public virtual void RecomDel()
        {
            string Ids = ctx.PostIdList("selectinfoid");
            string condition = string.Format("Id in ({0})", Ids);
            if (db.deleteBatch<Document_Label>(condition) > 0)
            {
                logService.Add((User)ctx.viewer.obj, lang("modelmanage_attr_log_del") + lang("log_extra_ok"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("modelmanage_attr_log_del") + lang("log_extra_no"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            ctx.web.ResponseEnd();
        }

    }
}
